package com.gxt.prod.controller;

import com.gxt.prod.service.IProductService;
import com.gxt.prod.domain.Product;
import com.gxt.prod.query.ProductQuery;
import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Api(value = "产品信息controller",description = "产品信息controller")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存产品信息")
    public AjaxResult save(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.insert(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除产品信息")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            productService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "查询产品详情")
    public AjaxResult loadById(@PathVariable("id")Long id)
    {
        try {
            Product product = productService.loadById(id);
            return AjaxResult.me().setResult(product);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }



    @GetMapping
    @ApiOperation(value = "查询所有产品")
    public AjaxResult getAll(){

        try {
            List< Product> list = productService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }



    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody ProductQuery query)
    {
        try {
            PageList<Product> pageList = productService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    //批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除产品")
    public AjaxResult batchDelete(@ApiParam(required = true) @RequestBody List<Long> ids){
        try {
            productService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

}
